using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Oplaco.Web.DTO;
using Oplaco.Web.UI;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;


public partial class AdvertismentsGrid_ascx : System.Web.UI.UserControl
{
    private Oplaco.Core.Configuration.SiteSettings settings = Oplaco.Core.Configuration.SiteSettings.GetSharedSettings();

    public String CurrentNavigatieUrl
    {
        get;
        set;
    }

    public bool IsSecure
    {
        get;
        set;
    }
    

    /// <summary>
    /// Loads data for the control. If an categoryid is set, it tries to load ads based on the categoryid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {

        CurrentNavigatieUrl = HttpContext.Current.Request.Url.AbsolutePath + "?cid=";
        int catId = 0;

        if (Page.Request.QueryString.Count > 0) {            
            catId = Convert.ToInt32(Page.Request.QueryString["cid"]);
        }

        
                      
        CachedAdvertismentCollection alist = null;
        this.loadCategoryNavigation();
        AdsList.Visible = true;

        if (this.IsSecure && Profile.MemberId > 0) {

            alist = this.loadAdvertisments(catId, Profile.MemberId, 100);            
        }
        else
        {
            alist = this.loadAdvertisments(catId, 0, 100);            
        }


        AdsList.DataSource = alist;
        AdsList.DataBind();   
        
    }

    protected void DisplayOptions_Click(object sender, EventArgs e)
    {

        LinkButton lnkBtn = (LinkButton)sender;
        CachedAdvertismentCollection alist = null;
        int catId = 0;
        bool withImage = lnkBtn.CommandArgument == "showAsImageList" ? true : false;


        if (this.IsSecure && Profile.MemberId > 0)
        {
            //if (catId == 0) alist = AdvertismentManager.FetchByAdLevel(HttpContext.Current, 100, AdLevel.Unspecified, withImage);
            alist = AdvertismentManager.FetchByQuery(catId, Profile.MemberId, 10000000, String.Empty, AdType.Unspecified, AdLevel.Unspecified, settings.MaxAdPrice, withImage, 100);
        }
        else {
            if (catId == 0) alist = AdvertismentManager.FetchByAdLevel(HttpContext.Current, 100, AdLevel.Unspecified, withImage);
            else alist = AdvertismentManager.FetchByQuery(catId, 0, 10000000, String.Empty, AdType.Unspecified, AdLevel.Unspecified, settings.MaxAdPrice, withImage, 100);
        }

        if (alist != null && alist.Count > 0)
        {
            if (withImage)
            {
                AdsList.Visible = false;
                AdImageList.Visible = true;
                AdImageList.DataSource = alist;
                AdImageList.DataBind();
            }
            else
            {
                AdsList.Visible = true;
                AdImageList.Visible = false;
                AdsList.DataSource = alist;
                AdsList.DataBind();
            }
        }

        this.loadCategoryNavigation();

    }

    private void loadCategoryNavigation() {
        //load all categories

        try
        {
            CachedCategoryCollection clist = CategoryManager.Fetch(HttpContext.Current, 100);
            if (clist != null && clist.Count > 0)
            {
                CategoryNavigation.DataSource = clist;
                CategoryNavigation.DataBind();
            }
        }
        catch (Exception exception)
        {
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
        }
    }

    private CachedAdvertismentCollection loadAdvertisments(int categoryId, int profileMemberId, int maximumItems) {
        try
        {
            //always try cached collection first
            if (categoryId == 0 && profileMemberId == 0) return AdvertismentManager.Fetch(HttpContext.Current, maximumItems);
            else return AdvertismentManager.FetchByQuery(categoryId, profileMemberId, 10000000, String.Empty, AdType.Unspecified, AdLevel.Unspecified, settings.MaxAdPrice, false, maximumItems);
        }
        catch (Exception exception) {
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
        }

        return null;
    }
}
